package com.codelazy.desetalk;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import com.liyh.widget.toRoundBitmap;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.bean.SocializeEntity;
import com.umeng.socialize.bean.SocializeUser;
import com.umeng.socialize.controller.RequestType;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners.FetchUserListener;
import com.umeng.socialize.controller.listener.SocializeListeners.SocializeClientListener;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class SetFragment extends Fragment {
	
	private Button btn_log;
	private Button btn_info;
	private Button btn_logout;
	private TextView tv_nick;
	private ImageView rimg;
	final UMSocialService mController = UMServiceFactory.getUMSocialService(
			"com.umeng.share", RequestType.SOCIAL);

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_set, container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		btn_log = (Button) getView().findViewById(R.id.btn_log);
		btn_logout = (Button) getView().findViewById(R.id.btn_logout);
		btn_info = (Button) getView().findViewById(R.id.btn_info);
		rimg=(ImageView)getActivity()
				.findViewById(R.id.rimg);
		btn_log.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				mController.login(getActivity(), SHARE_MEDIA.SINA,
						new SocializeClientListener() {

							@Override
							public void onStart() {
								// TODO Auto-generated method stub

							}

							@Override
							public void onComplete(int arg0,
									SocializeEntity arg1) {
								// TODO Auto-generated method stub
								if (arg0 == 200) {
									Toast.makeText(getActivity(), "登陆成功",
											Toast.LENGTH_SHORT).show();

								} else {
									Toast.makeText(getActivity(), "登陆失败",
											Toast.LENGTH_SHORT).show();
								}

							}
						});

			}
		});
		btn_logout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mController.loginout(getActivity(),
						new SocializeClientListener() {

							@Override
							public void onStart() {
								// TODO Auto-generated method stub

							}

							@Override
							public void onComplete(int arg0,
									SocializeEntity arg1) {
								// TODO Auto-generated method stub
								if (arg0 == 200) {
									Toast.makeText(getActivity(), "ע��ɹ�",
											Toast.LENGTH_SHORT).show();
								} else {
									Toast.makeText(getActivity(), "ע��ʧ��",
											Toast.LENGTH_SHORT).show();
								}
							}
						});
			}
		});
		btn_info.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				mController.getUserInfo(getActivity(), new FetchUserListener() {

					@Override
					public void onStart() {
						// TODO Auto-generated method stub

					}

					@Override
					public void onComplete(int arg0, SocializeUser arg1) {
						// TODO Auto-generated method stub

						String uname = arg1.mLoginAccount.getUserName();
						String iconurl = arg1.mLoginAccount.getAccountIconUrl();
						Bitmap bmBitmap = toRoundBitmap.getbit(returnBitMap(iconurl));
						rimg.setImageBitmap(bmBitmap);
						tv_nick = (TextView) getActivity().findViewById(
								R.id.tv_nick);
						tv_nick.setText(uname);
					}
				});
			}
		});

	}

	public Bitmap returnBitMap(String url) {
		URL myFileUrl = null;
		Bitmap bitmap = null;
		try {
			myFileUrl = new URL(url);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		try {
			HttpURLConnection conn = (HttpURLConnection) myFileUrl
					.openConnection();
			conn.setDoInput(true);
			conn.connect();
			InputStream is = conn.getInputStream();
			bitmap = BitmapFactory.decodeStream(is);
			is.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return bitmap;
	}
}
